import { Injectable } from '@nestjs/common';
import { OrderEntity } from '../entitys/order.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import uuidCreate from '../utils/uuidCreate'
import { orderNumberCreate } from '../utils/orderNumberCreate'
import * as dayjs from 'dayjs'

@Injectable()
export class OrderService {
  constructor (
    @InjectRepository(OrderEntity)
    private readonly ordersRepository: Repository<OrderEntity>,
  ) { }

  // 创建订单
  async createOrderpayService(createOrderField, userId): Promise<any> {
    const f_order_id = uuidCreate()
    const f_order_price = createOrderField.fOrderPrice
    const f_create_time = dayjs(new Date()).format('YYYY-MM-DDTHH:mm:ss')
    const f_user_id = userId
    const f_state = '0'
    const f_is_del = '0'
    const f_product_id = createOrderField.fProductId
    const f_order_no = orderNumberCreate()
    const sql = 'INSERT INTO t_order_pay(f_order_id, f_order_price, f_create_time, f_user_id, f_state, f_is_del, f_product_id, f_order_no) VALUE(?,?,?,?,?,?,?,?)'
    try {
      const result = await this.ordersRepository.query(sql, [f_order_id, f_order_price, f_create_time, f_user_id, f_state, f_is_del, f_product_id, f_order_no])
      if (result.affectedRows === 1) {
        return {
          messsage: '订单创建成功'
        }
      }
    } catch (error) {
      console.log(error)
    }
  }
}